package com.scvtc.service.research.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scvtc.domain.ResResult;
import com.scvtc.domain.dto.research.page.ResearchPaperDto;
import com.scvtc.domain.entity.research.ResearchPaper;
import com.scvtc.domain.vo.PageVo;
import com.scvtc.domain.vo.research.page.ResearchPaperVo;
import com.scvtc.mapper.research.ResearchPaperMapper;
import com.scvtc.service.research.ResearchPaperService;
import com.scvtc.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 科研刊物信息(ResearchPaper)表服务实现类
 *
 * @author yiFei
 * @since 2022-06-02 16:17:09
 */
@Service("researchPaperService")
public class ResearchPaperServiceImpl extends ServiceImpl<ResearchPaperMapper, ResearchPaper> implements ResearchPaperService {
    /**
     * 查询科研刊物信息
     *
     * @param current
     * @param size
     * @param researchPaperVo
     * @return
     */
    @Override
    public ResResult<PageVo<List<ResearchPaperVo>>> getAllResearchPaper(Long current, Long size, ResearchPaperVo researchPaperVo) {
        // 分页查询
        Page<ResearchPaper> page = getBaseMapper().getAllResearchPaper(new Page<ResearchPaper>(current, size), researchPaperVo);
        // BeanCopy
        List<ResearchPaperVo> researchPaperVoList = BeanCopyUtils.copyBean(page.getRecords(), ResearchPaperVo.class);
        // 返回成功值
        return ResResult.noMsgSuccess(new PageVo<>(researchPaperVoList, page.getTotal()));
    }

    /**
     * 添加科研刊物信息
     *
     * @param researchPaperDto
     * @return
     */
    @Override
    public ResResult<?> addResearchPaper(ResearchPaperDto researchPaperDto) {
        // 如果考虑后端安全性需要判断教工号是否存在 (代码可参考学生获奖表)
        ResearchPaper researchPaper = BeanCopyUtils.copyBean(researchPaperDto, ResearchPaper.class);
        // 判断是否成功
        if (save(researchPaper)) {
            return ResResult.success(researchPaper.getId());
        }
        return ResResult.fail();
    }

    /**
     * 修改科研刊物信息
     *
     * @param researchPaperDto
     * @return
     */
    @Override
    public ResResult<?> updateResearchPaper(ResearchPaperDto researchPaperDto) {
        // 如果考虑后端安全性需要判断教工号是否存在 (代码可参考学生获奖表)
        ResearchPaper researchPaper = BeanCopyUtils.copyBean(researchPaperDto, ResearchPaper.class);
        // 判断是否成功
        if (updateById(researchPaper)) {
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 删除单个科研刊物信息
     *
     * @param id
     * @return
     */
    @Override
    public ResResult<?> deleteResearchPaper(Long id) {
        // 判断是否成功
        if (removeById(id)) {
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 删除多个科研刊物信息
     *
     * @param ids
     * @return
     */
    @Override
    public ResResult<?> deleteMultipleResearchPaper(Long[] ids) {
        // 判断是否成功
        if (removeByIds(Arrays.asList(ids))) {
            return ResResult.success();
        }
        return ResResult.fail();
    }
}

